// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init(
  {
    env: 'sungodd00-bf5531'
  }
)

const rp = require('request-promise')

const URL = 'http://cgj.vaiwan.com/personalized';

const db = cloud.database();
const playlistCollection = db.collection("playlist");
// 云函数入口函数
const MAX_LIMIT = 100;

exports.main = async (event, context) => {
  // const list = await playlistCollection.get()
  // 获取所有的长度
  const countResult = await playlistCollection.count();
  const total = countResult.total;

  const batchTimes = Math.ceil(total / MAX_LIMIT);
  const tasks = [];

  for(let i =0;i<batchTimes;i++) {
    let promise = playlistCollection.skip(i * MAX_LIMIT).limit(MAX_LIMIT).get();
    tasks.push(promise);
  }

  let list = {
    data: []
  }
  if(tasks.length > 0) {
    list = (await Promise.all(tasks)).reduce((acc,cur)=> {
      return {
        data: acc.data.concat(cur.data)
      }
    });
  }

  const playlist = await rp(URL).then(res => {
    return JSON.parse(res).result;
  })

  const newData = [];
  for (let i = 0; i < playlist.length; i++) {
    let flag = true;
    for (let j = 0; j < list.data.length; j++) {
      if (playlist[i].id === list.data[j].id) {
        flag = false;
        break;
      }
    }
    if (flag) {
      newData.push(playlist[i])
    }
  }

  for (let i = 0; i < newData.length; i++) {
    await playlistCollection.add({
      data: {
        ...newData[i],
        createTime: db.serverDate()
      }
    }).then(res => {
      console.log("插入成功")
    }).catch(err => {
      console.log("删除失败");
    })
  }

  return newData.length;

}